package cn.fl.infside.service.impl;

import cn.cmm.base.framework.context.ApplicationContextAccessor;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.fl.infside.service.PayResultInterface;
import cn.fl.infside.utils.ServiceXmlUtils;
import fl.inf.facade.api.IBFSApiService;
import fl.inf.facade.dto.InfBfsPayDTO;
import fl.inf.facade.dto.MessageDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 付款结果
 */
public class PayResultServiceImpl implements PayResultInterface {

    private static final Logger logger = LoggerFactory.getLogger(PayResultServiceImpl.class);

    /**
     * 付款成功结果反馈
     */
    @Override
    public String updatePayinfoSucceed(String business) throws Exception {
        logger.info("updatePayinfo====更新开始business================" + business);
        List<Map<String, Object>> list = ServiceXmlUtils.xmlElements(business);
        StringBuffer rs = new StringBuffer("");
        if (list != null && list.size() > 0) {
            rs.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>");
            for (int i = 0; i < list.size(); i++) {
                boolean flag = true;
                StringBuffer resultMsg = new StringBuffer("");
                rs.append("	<row>");
                Map<String, Object> map = list.get(i);
                if (map.get("serialid") == null || map.get("serialid").equals("")) {
                    flag = false;
                    resultMsg.append("serialid空");
                } else {
                    rs.append("<serialid>").append(map.get("serialid").toString()).append("</serialid>");
                }
                if (map.get("payer_acc_no") == null || map.get("payer_acc_no").equals("")) {
                    flag = false;
                    resultMsg.append("payer_acc_no空");
                }
                if (map.get("payer_acc_name") == null || map.get("payer_acc_name").equals("")) {
                    flag = false;
                    resultMsg.append("payer_acc_name空");
                }
                if (map.get("payer_bank_name") == null || map.get("payer_bank_name").equals("")) {
                    flag = false;
                    resultMsg.append("payer_bank_name空");
                }
                if (map.get("payer_bank_code") == null || map.get("payer_bank_code").equals("")) {
                    flag = false;
                    resultMsg.append("payer_bank_code空");
                }
                if (map.get("pay_money") == null || map.get("pay_money").equals("")) {
                    flag = false;
                    resultMsg.append("pay_money空");
                } else {
                    try {
                        double d = Double.parseDouble(map.get("pay_money").toString());
                    } catch (Exception e) {
                        // TODO: handle exception
                        BaseLogUtils.error(logger, "-", "异常", e);
                        flag = false;
                        resultMsg.append("pay_money格式错误");
                    }
                }
                if (map.get("pay_date") == null || map.get("pay_date").equals("")) {
                    flag = false;
                    resultMsg.append("pay_date空");
                } else if (map.get("pay_date").toString().length() != 10) {
                    flag = false;
                    resultMsg.append("pay_date格式错误");
                }
                if (flag) {
                    Boolean rn = false;
                    InfBfsPayDTO payDto = new InfBfsPayDTO();
                    payDto.setId(Long.valueOf(map.get("serialid").toString()));
                    payDto.setPayerAccname(map.get("payer_acc_name").toString());
                    payDto.setPayerAcc(map.get("payer_acc_no").toString());
                    payDto.setPayerBank(map.get("payer_bank_name").toString());
                    payDto.setBifCode(map.get("payer_bank_code").toString());
                    payDto.setPayMoneyReal(DecimalUtils.convertYuanToFen(Double.valueOf(map.get("pay_money").toString())));
                    payDto.setPayDate(DateUtils.formatStringToDate(map.get("pay_date").toString(), "yyyy-MM-dd"));
                    IBFSApiService bFSApiService = (IBFSApiService) ApplicationContextAccessor.getBean("bFSApiService");
                    MessageDTO messagedto = bFSApiService.paySuccess(payDto);
                    rn = messagedto.isSuccess();
                    logger.info("数据库返回数据:" + messagedto.getMessage());
                    if (flag && rn) {
                        rs.append("<result_code>").append("S").append("</result_code>");
                        rs.append("<result_msg>").append("成功").append("</result_msg>");

                    } else {
                        rs.append("<result_code>").append("F").append("</result_code>");
                        rs.append("<result_msg>").append("数据库保存错误").append("</result_msg>");
                    }
                } else {
                    rs.append("<result_code>").append("F").append("</result_code>");
                    rs.append("<result_msg>").append(resultMsg).append("</result_msg>");
                }
                rs.append("<reserve2>").append(map.get("reserve2").toString()).append("</reserve2>");
                rs.append("	</row>");
            }
            rs.append("</root>");
        } else {
            return "解析错误";
        }
        logger.info("返回数据: " + rs.toString());
        return rs.toString();
    }


    /**
     * 付款失败结果反馈
     * 1：打回修改
     * 2：作废
     */
    @Override
    public String updatePayinfoFailure(String business) throws Exception {
        logger.info("updatePayinfo====更新开始business================" + business);
        List<Map<String, Object>> list = ServiceXmlUtils.xmlElements(business);
        StringBuffer rs = new StringBuffer("");
        if (list != null && list.size() > 0) {
            rs.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>");
            for (int i = 0; i < list.size(); i++) {
                boolean flag = true;
                StringBuffer resultMsg = new StringBuffer("");
                rs.append("	<row>");
                Map<String, Object> map = list.get(i);
                if (map.get("serialid") == null || map.get("serialid").equals("")) {
                    flag = false;
                    resultMsg.append("serialid空");
                } else {
                    rs.append("<serialid>").append(map.get("serialid").toString()).append("</serialid>");
                }

                if (map.get("res_msg") == null || map.get("res_msg").equals("")) {
                    flag = false;
                    resultMsg.append("res_msg空");
                }
                String opType = map.get("op_type").toString().trim();
                if (opType == null || "".equals(opType)) {
                    flag = false;
                    resultMsg.append("op_type空");
                    //1打回修改 2打回作废
                } else if (!"1".equals(opType) && !"2".equals(opType)) {
                    flag = false;
                    resultMsg.append("op_type错误");
                }
                if (flag) {
                    Boolean rn = false;
                    IBFSApiService bFSApiService = (IBFSApiService) ApplicationContextAccessor.getBean("bFSApiService");
                    if ("1".equals(opType)) { //1打回修改
                        //修改前先清空
                        bFSApiService.clearPayInfo(Long.valueOf(map.get("serialid").toString()));
                        InfBfsPayDTO payDto = new InfBfsPayDTO();
                        payDto.setId(Long.valueOf(map.get("serialid").toString()));
                        payDto.setMessage(map.get("res_msg").toString());
                        MessageDTO messagedto = bFSApiService.rejectUpdate(payDto);
                        rn = messagedto.isSuccess();
                    } else if ("2".equals(opType)) { //2打回作废
                        InfBfsPayDTO payDto = new InfBfsPayDTO();
                        payDto.setId(Long.valueOf(map.get("serialid").toString()));
                        payDto.setMessage(map.get("res_msg").toString());
                        MessageDTO messagedto = bFSApiService.cancel(payDto);
                        rn = messagedto.isSuccess();
                    }

                    if (flag && rn) {
                        rs.append("<result_code>").append("S").append("</result_code>");
                        rs.append("<result_msg>").append("成功").append("</result_msg>");

                    } else {
                        rs.append("<result_code>").append("F").append("</result_code>");
                        rs.append("<result_msg>").append("数据库保存错误").append("</result_msg>");
                    }
                } else {
                    rs.append("<result_code>").append("F").append("</result_code>");
                    rs.append("<result_msg>").append(resultMsg).append("</result_msg>");
                }
                rs.append("<reserve2>").append(map.get("reserve2").toString()).append("</reserve2>");
                rs.append("	</row>");
            }
            rs.append("</root>");
        } else {
            return "解析错误";
        }
        logger.info("返回数据: " + rs.toString());
        return rs.toString();
    }
}
